/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package itb.nlp.gate.featureextraction;

import gate.Annotation;
import gate.AnnotationSet;
import gate.Document;
import gate.ProcessingResource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.ExecutionException;
import gate.util.OffsetComparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author asatrya
 */
public class PositionExtractor extends AbstractLanguageAnalyser implements ProcessingResource {
    String inputASName, outputASName;

    public String getinputASName() {
        return inputASName;
    }

    public void setinputASName(String inputASName) {
        this.inputASName = inputASName;
    }

    public String getoutputASName() {
        return outputASName;
    }

    public void setoutputASName(String outputASName) {
        this.outputASName = outputASName;
    }

    @Override
    public void execute() throws ExecutionException {
        Document doc = getDocument();

        AnnotationSet inputAnnSet = (inputASName == null || inputASName.length() == 0)
                ? doc.getAnnotations()
                : doc.getAnnotations(inputASName);

        AnnotationSet outputAnnSet = (outputASName == null || outputASName.length() == 0)
                ? doc.getAnnotations()
                : doc.getAnnotations(outputASName);

        //Get sentence annotation types
        AnnotationSet sentenceAnnSet = inputAnnSet.get("Sentence");
        List<Annotation> sentenceAnnList = new ArrayList<Annotation>(sentenceAnnSet);
        Collections.sort(sentenceAnnList, new OffsetComparator());

        //Get token annotation types
        AnnotationSet tokenAnnSet = inputAnnSet.get("Token");

        //Counters
        int sentenceInDocCounter = 1;
        int tokenInSentenceCounter;
        int tokenInDocCounter = 1;

        //Iterate sentence annotation types
        ListIterator<Annotation> sentenceAnnIter = sentenceAnnList.listIterator();
        while(sentenceAnnIter.hasNext()){
            //Put sentence position in document
            Annotation tSentenceAnn = sentenceAnnIter.next();
            tSentenceAnn.getFeatures().put("positionInDoc", sentenceInDocCounter);
            sentenceInDocCounter++;

            //Get contained token in the sentence
            AnnotationSet containedTokenAnnSet = tokenAnnSet.getContained(
                    tSentenceAnn.getStartNode().getOffset(),
                    tSentenceAnn.getEndNode().getOffset());

            //Iterate contained token annotation set
            List<Annotation> tokenAnnList = new ArrayList<Annotation>(containedTokenAnnSet);
            Collections.sort(tokenAnnList, new OffsetComparator());
            ListIterator<Annotation> tokenAnnIter = tokenAnnList.listIterator();

            tokenInSentenceCounter = 1;
            while(tokenAnnIter.hasNext()){
                Annotation tTokenAnn = tokenAnnIter.next();
                tTokenAnn.getFeatures().put("positionInSentence", tokenInSentenceCounter);
                tTokenAnn.getFeatures().put("positionInDoc", tokenInDocCounter);

                tokenInSentenceCounter++;
                tokenInDocCounter++;
            }
        }
    }
}
